#include "CCAttachment.h"
#include <cstdlib>
#include <cstring>
#include "CCSpineGlobal.h"

NS_CC_BEGIN

CCAttachment::CCAttachment() :
m_x(0),
m_y(0),
m_rotation(0),
m_scaleX(1),
m_scaleY(1),
m_path(NULL) {
}

CCAttachment::~CCAttachment() {
    if (m_path) {
        free((void*)m_path);
    }
}

CCAttachment* CCAttachment::create() {
	CCAttachment* a = new CCAttachment;
	return (CCAttachment*)a->autorelease();
}

void CCAttachment::setPath(const char* path) {
    if(m_path) {
	    free((void*) m_path);
    }
    m_path=CCSpineGlobal::copy(path);
}

void CCAttachment::synchro(cocos2d::CCNode *node) {
    node->setPosition(ccp(m_x, m_y));
    node->setRotation(-m_rotation);
    node->setScaleX(m_scaleX);
    node->setScaleY(m_scaleY);
}

CCSpriteFrame* CCAttachment::createRelatedSpriteFrame() {
	CCSpriteFrame* ret = nullptr;
	if(strlen(m_path) > 0) {
		char fileName[64];
	    sprintf(fileName, "%s.png", m_path);
		ret = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fileName);
	}
	return ret;
}

NS_CC_END